package com.zhichat.web.controller;

import com.zhichat.infrastructure.exception.http.CommonException;
import com.zhichat.service.MessageService;
import com.zhichat.web.api.R;
import com.zhichat.web.dto.MsgDTO;
import com.zhichat.web.dto.MsgUploadFileDTO;
import com.zhichat.web.vo.ws.MsgVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消息功能
 *
 * @Author: 团长九哥 https://space.bilibili.com/602197686
 * @CreateDate: 2024/12/18 9:45
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/message")
public class MessageController {

    private final MessageService messageService;

    /**
     * 发送消息
     */
    @PostMapping("/sendMsg")
    public R<MsgVO<String>> sendMsg(@Validated MsgDTO dto) {
        return R.success(messageService.sendMsg(dto));
    }

    /**
     * 上传文件
     */
    @PostMapping("/uploadFile")
    public R<String> uploadFile(@Validated MsgUploadFileDTO dto) {
        return R.status(messageService.uploadFile(dto));
    }

    /**
     * 文件上传成功回调接口
     */
    @PostMapping("/sendFileSucCallback")
    public R<String> sendFileSucCallback(HttpServletRequest request) {
        return R.status(messageService.sendFileSucCallback(request));
    }

    /**
     * 获取文件临时下载url
     */
    @GetMapping("/downloadFile")
    public R<List<String>> downloadFileMsg(@RequestParam(value = "id") Long id,
                                           @RequestParam(value = "type") Integer type,
                                           @RequestParam(value = "isCover", defaultValue = "false") Boolean isCover) {
        return R.success(messageService.downloadFileMsg(id, type, isCover));
    }

    /**
     * 流式下载文件
     */
    @GetMapping("/downloadFile1")
    public void downloadFileMsg1(@RequestParam(value = "id") String id,
                                 @RequestParam(value = "type") Integer type,
                                 @RequestParam(value = "isCover", defaultValue = "false") Boolean isCover,
                                 HttpServletResponse response) {
        try {
            messageService.downloadFileMsg1(id, type, isCover, response);
        } catch (Exception e) {
            log.error("下载文件异常", e);
            throw CommonException.FILE_OPERATE_COMMON_EXCEPTION;
        }
    }

    /**
     * 获取消息文件
     */
    @GetMapping("/downloadFile2")
    public R<List<String>> downloadMsgFileMsg(@RequestParam(value = "id") String id,
                                              @RequestParam(value = "isCover", defaultValue = "false") Boolean isCover,
                                              HttpServletResponse response) {
        try {
            messageService.downloadMsgFileMsg(id, isCover, response);
        } catch (Exception e) {
            log.error("下载文件异常", e);
            throw CommonException.FILE_OPERATE_COMMON_EXCEPTION;
        }
        return R.success();
    }
}
